package com.exina.android.calendar;

import tpaagp.scheduleus.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarActivity extends Activity  implements CalendarView.OnCellTouchListener{
	public static final String MIME_TYPE = "vnd.android.cursor.dir/vnd.exina.android.calendar.date";
	CalendarView mView = null;
	TextView mHit;
	Handler mHandler = new Handler();
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar);
        mView = (CalendarView)findViewById(R.id.calendar);
        mView.setOnCellTouchListener(this);
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		String uri = prefs.getString("URL", "http://127.0.0.1/");
		String user = prefs.getString("Name", "defValue");
		System.out.println(uri+"consulta_programados.php?user="+user);
		// Set the URL of the web service
        mView.setEvents(uri+"consulta_programados.php?user="+user);
    }

    
	public void onTouch(Cell cell) {
		Intent intent = getIntent();
		String action = intent.getAction();
		if(action.equals(Intent.ACTION_PICK) || action.equals(Intent.ACTION_GET_CONTENT)) {
			Intent ret = new Intent();
			ret.putExtra("year", mView.getYear());
			ret.putExtra("month", mView.getMonth());
			ret.putExtra("day", cell.getDayOfMonth());
			ret.putExtra("event", cell.getEvent());
			this.setResult(RESULT_OK, ret);
			finish();
			return;
		}
		int day = cell.getDayOfMonth();
		int month = mView.getMonth();
		boolean thisMonth = cell.getThisMonth();
		int event = cell.getEvent();
		if (event != -1) {
			alertEvent(event, day, month);
		}
		if(mView.firstDay(day) && thisMonth)
			mView.previousMonth();
		else if(mView.lastDay(day)&& thisMonth)
			mView.nextMonth();
		else
			return;
		
		mHandler.post(new Runnable() {
			public void run() {
				Toast.makeText(CalendarActivity.this, DateUtils.getMonthString(mView.getMonth(), DateUtils.LENGTH_LONG) + " "+mView.getYear(), Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	private void alertEvent(int h, int day, int month) {
		AlertDialog.Builder a = new AlertDialog.Builder(this);
		String msg = "You have a meeting the ";
		msg = msg.concat(Integer.toString(day));
		switch(day) {
			case 0:
				msg = msg.concat("st");
				break;
			case 1:
				msg = msg.concat("nd");
				break;
			case 2:
				msg = msg.concat("rd");
				break;
			default:
				msg = msg.concat("th");
				break;
		}
		
		switch(month) {
			case 0:
				msg = msg.concat(" of January");
				break;
			case 1:
				msg = msg.concat(" of February");
				break;
			case 2:
				msg = msg.concat(" of March");
				break;
			case 3:
				msg = msg.concat(" of April");
				break;
			case 4:
				msg = msg.concat(" of May");
				break;
			case 5:
				msg = msg.concat(" of June");
				break;
			case 6:
				msg = msg.concat(" of July");
				break;
			case 7:
				msg = msg.concat(" of August");
				break;
			case 8:
				msg = msg.concat(" of September");
				break;
			case 9:
				msg = msg.concat(" of October");
				break;
			case 10:
				msg = msg.concat(" of November");
				break;
			case 11:
				msg = msg.concat(" of December");
				break;
			default:
				break;
		}
		
		msg = msg.concat(" at ").concat(Integer.toString(h));
		SharedPreferences prefs = PreferenceManager
		        .getDefaultSharedPreferences(getApplicationContext());
		String group = prefs.getString("workgroup", "workgroup");
		msg = msg.concat(":00h with <").concat(group).concat(">");
		a.setMessage(msg).setPositiveButton("Accept",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		a.show();
	}

    
}